package top.hkyzf.study.j2se.juc.locks;

import java.util.concurrent.TimeUnit;

/**
 * @author 朱峰
 * @date 2022-1-19 16:14
 */
public class DeadLockDemo {
    private static Object resourse1 = new Object();
    private static Object resourse2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (resourse1) {
                System.out.println(Thread.currentThread().getName() + " : lock resource1");
                try {
                    System.out.println(Thread.currentThread().getName() + " : try to lock resource2");
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resourse2) {
                    System.out.println(Thread.currentThread().getName() + " : lock resource2");
                }
            }

        }, "T1").start();
        new Thread(() -> {
            synchronized (resourse2) {
                System.out.println(Thread.currentThread().getName() + " : lock resource2");
                try {
                    System.out.println(Thread.currentThread().getName() + " : try to lock resource1");
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resourse1) {
                    System.out.println(Thread.currentThread().getName() + " : lock resource1");
                }
            }
        }, "T2").start();
    }
}
